package com.moorror.codegenvel.utils;

import com.intellij.openapi.project.Project;
import com.moorror.codegenvel.common.tree.MTTreeCell;
import com.moorror.codegenvel.db.sqlite.entity.*;
import com.moorror.codegenvel.view.window.codegen.tree.MTTreeCellCodeGenData;
import com.moorror.codegenvel.view.window.codegen.tree.MTTreeCellCodeGenScene;
import com.moorror.codegenvel.view.window.codegenxrender.tree.MTTreeCellCodeGenXRenderConnectName;
import com.moorror.codegenvel.view.window.codegenxrender.tree.MTTreeCellCodeGenXRenderNameTable;
import com.moorror.codegenvel.view.window.codegenxrender.tree.MTTreeCellCodeGenXRenderNameTableConfig;
import com.moorror.codegenvel.view.window.history.tree.MTTreeCellHistoryScene;
import org.apache.commons.collections4.CollectionUtils;

import javax.swing.tree.DefaultMutableTreeNode;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author jiyanghuang
 * @Date 2025/8/16 09:11
 */
public class TreeCellEntityUtils {

    private static final Map<Class<? extends InfTreeCellEntity>, Class<? extends MTTreeCell>> entityTreeCellMap = new HashMap();

    static {
        entityTreeCellMap.put(CodeGenScene.class, MTTreeCellCodeGenScene.class);
        entityTreeCellMap.put(CodeGenTemplate.class, MTTreeCellCodeGenData.class);

        entityTreeCellMap.put(CodeGenXRenderConnect.class, MTTreeCellCodeGenXRenderConnectName.class);
        entityTreeCellMap.put(CodeGenXRenderTable.class, MTTreeCellCodeGenXRenderNameTable.class);
        entityTreeCellMap.put(CodeGenXRenderTableConfig.class, MTTreeCellCodeGenXRenderNameTableConfig.class);
        entityTreeCellMap.put(CommonHistory.class, MTTreeCellHistoryScene.class);
    }

    /**
     * 约定一定得有 project 与 对就 Entity 对象的构造方法
     * @param project
     * @param en
     * @return
     * @param <T>
     * @param <E>
     */
    public static <T extends MTTreeCell, E extends InfTreeCellEntity> T genMTTreeCell(Project project, E en){
        try{
            Class<? extends MTTreeCell> clazz = entityTreeCellMap.get(en.getClass());
            Constructor<T> constructor =  (Constructor)clazz.getConstructor(Project.class, en.getClass());
            return constructor.newInstance(project, en);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }


    /**
     * 要目录需要先清空原来的树
     * @param rootTreeNode
     * @param project
     * @param roots
     * @param <T>
     * @param <E>
     */
    public static <T extends MTTreeCell, E extends InfTreeCellEntity> void genTreeRoot(DefaultMutableTreeNode rootTreeNode, Project project, List<E> roots) {
        rootTreeNode.removeAllChildren();
        genTreeRecursion(rootTreeNode, project, roots);
    }

    /**
     * 要目录需要先清空原来的树
     * @param rootTreeNode
     * @param project
     * @param roots
     * @param <T>
     * @param <E>
     */
    public static <T extends MTTreeCell, E extends InfTreeCellEntity> void genTreeRootAppend(DefaultMutableTreeNode rootTreeNode, Project project, List<E> roots) {
        genTreeRecursion(rootTreeNode, project, roots);
    }

    /**
     * 每次遍历一层生成树
     * @param parent
     * @param project
     * @param currents
     * @param <T>
     * @param <E>
     */
    public static <T extends MTTreeCell, E extends InfTreeCellEntity> void genTreeRecursion(DefaultMutableTreeNode parent, Project project, List<E> currents) {
        if (CollectionUtils.isEmpty(currents)) {
            return;
        }
        for (E e : currents) {
            T t = TreeCellEntityUtils.genMTTreeCell(project, e);
            if (null == t) {
                continue;
            }
            DefaultMutableTreeNode sonNode = new DefaultMutableTreeNode(t);
            parent.add(sonNode);
            genTreeRecursion(sonNode, project, e.getChildren());
        }
    }
}
